﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGourd.Core.Reports;

namespace NGourd.Core.Plans
{
    class MultiStepPlan : IPlan
    {
        private readonly IList<IPlan> PlanSteps = new List<IPlan>();
        private readonly ReportTarget reportTarget;
        private readonly string name;

        public MultiStepPlan(string name, ReportTarget reportTarget)
        {
            this.name = name;
            this.reportTarget = reportTarget;
        }

        public void AddStep(IPlan step)
        {
            PlanSteps.Add(step);
        }

        #region IPlan Members

        public ReportNode Execute(bool skipExecution)
        {
            var node = new ReportNode(reportTarget, name);
            bool skipping = skipExecution;
            foreach (var step in PlanSteps)
            {
                ReportNode result;
                if (step is LifecycleScenarioStep)
                {
                    result = step.Execute(skipExecution);
                }
                else
                {
                    result = step.Execute(skipping);
                }

                if (!skipping)
                {
                    var needToSKip = result.Status != ReportStatus.PASSED && reportTarget == ReportTarget.SCENARIO;
                    skipping = needToSKip || skipping;
                }

                node.Nodes.Add(result);
            }
            return node;
        }

        #endregion
    }
}
